package com.nengchuan.kbms.dfs.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nengchuan.excel.util.CsncExcelUtil;
import com.nengchuan.kbms.dfs.entity.dto.ManufacturerDTO;
import com.nengchuan.kbms.dfs.entity.dto.ManufacturerInfoDTO;
import com.nengchuan.kbms.dfs.entity.dto.ManufacturerInfoListDTO;
import com.nengchuan.kbms.dfs.entity.dto.ManufacturerInfoPageDTO;
import com.nengchuan.kbms.dfs.entity.po.Manufacturer;
import com.nengchuan.kbms.dfs.entity.po.ManufacturerInfo;
import com.nengchuan.kbms.dfs.entity.vo.ManufacturerListVO;
import com.nengchuan.kbms.dfs.entity.vo.ManufacturerInfoVO;
import com.nengchuan.kbms.dfs.service.ManufacturerInfoService;
import com.nengchuan.kbms.dfs.service.ManufacturerService;
import com.nengchuan.model.model.NcPageInfo;
import com.nengchuan.model.model.WrappedResult;
import com.nengchuan.util.MapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 供应商 前端控制器
 * </p>
 *
 * @author abe
 * @since 2020-03-24
 */
@RestController
@RequestMapping("/manufacturer")
@Api(tags = "知识库数据后台管理 > 供应商管理 【已完成】")
public class ManufacturerController {

    @Autowired
    private ManufacturerService manufacturerService;

    @Autowired
    private ManufacturerInfoService manufacturerInfoService;

    @PostMapping("/save")
    @ApiOperation(value = "供应商-新增（导航右键新增节点）")
    public WrappedResult<Long> save(@RequestBody ManufacturerDTO dto) {
        return WrappedResult.success(manufacturerService.insert(dto));
    }

    @PostMapping("/update")
    @ApiOperation(value = "供应商-修改（导航右键修改节点）")
    public WrappedResult<Boolean> update(@RequestBody ManufacturerDTO vo) {
        return WrappedResult.success(manufacturerService.update(vo));
    }

    @PostMapping("/list")
    @ApiOperation(value = "供应商-名称列表")
    public WrappedResult<List<ManufacturerListVO>> list() {
        return WrappedResult.success(MapperUtils.mappingList(manufacturerService.list(), ManufacturerListVO.class));
    }

    @PostMapping("/delete")
    @ApiOperation(value = "供应商-删除")
    public WrappedResult<Boolean> delete(@ApiParam(value = "供应商id", required = true) @RequestParam Serializable id) {
       return WrappedResult.success(manufacturerService.remove(id));
    }


    // ------------------------------------ 供应商售后信息分隔符

    @GetMapping("/info/data")
    @ApiOperation(value = "供应商售后-查询")
    public WrappedResult<ManufacturerInfoVO> data(@ApiParam(value = "供应商售后id", required = true) @RequestParam Long id) {
        ManufacturerInfoVO manufacturerInfoVO = manufacturerInfoService.getById(id, ManufacturerInfoVO.class);
        if (manufacturerInfoVO == null) {
            return WrappedResult.fail("错误的售后id");
        }
        Manufacturer manufacturer = manufacturerService.getById(manufacturerInfoVO.getManufacturerId());
        if (manufacturer != null) {
            manufacturerInfoVO.setManufacturerName(manufacturer.getManufacturerName());
        }
        return WrappedResult.success(manufacturerInfoVO);
    }

    @PostMapping("/info/save")
    @ApiOperation(value = "供应商售后-保存")
    public WrappedResult<Long> infoSave(@RequestBody ManufacturerInfoDTO dto) {
        ManufacturerInfo manufacturerInfo = manufacturerInfoService.insert(dto);
        return WrappedResult.success(manufacturerInfo.getId());
    }

    @PostMapping("/info/update")
    @ApiOperation(value = "供应商售后-修改")
    public WrappedResult<Boolean> infoUpdate(@RequestBody ManufacturerInfoDTO vo) {
        return WrappedResult.success(manufacturerInfoService.update(vo));
    }

    @PostMapping("/info/delete")
    @ApiOperation(value = "供应商售后-删除")
    public WrappedResult<Boolean> infoDelete(@ApiParam(value = "供应商售后id", required = true) @RequestParam Serializable id) {
        return WrappedResult.success(manufacturerInfoService.remove(id));
    }

    @PostMapping("/info/page")
    @ApiOperation(value = "供应商售后-分页")
    public WrappedResult<NcPageInfo<ManufacturerInfoVO>> infoPage(@RequestBody ManufacturerInfoPageDTO manufacturerInfoPageDTO) {
        return WrappedResult.success(MapperUtils.mappingNcPage(manufacturerInfoService.infoPage(manufacturerInfoPageDTO), ManufacturerInfoVO.class));
    }

    @PostMapping(value = "/info/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation(value = "供应商售后-导出")
    public void infoExport(@RequestBody ManufacturerInfoListDTO manufacturerInfoListDTO, HttpServletResponse response) throws Exception {
        List<ManufacturerInfoVO> list = manufacturerInfoService.infoList(manufacturerInfoListDTO);
        CsncExcelUtil.export(list, ManufacturerInfoVO.class, response, "供应商售后列表.xls");
    }

}
